<?php

// +-------------------------------------------------
// | Name: 星空框架 上传控制器
// +-------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------
// | Time: 2021-04-15 09:23
// +-------------------------------------------------
declare(strict_types=1);

namespace app\common\controller;

use app\common\common\BaseController;
use app\common\services\UploadServices;

/**
 * @xk 图片上传
 */
class Upload extends BaseController
{
    /**
     * @xk 图片上传
     *
     * @method uploadImg
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-15T09:27:06+080
     */
    public function uploadImg(UploadServices $uploadServices)
    {
        $upload_file = $_FILES['file'];
        // 获取图片基础信息
        $picsize = $upload_file['size'];
        $pictype = $upload_file['type'];
        $pictemp = $upload_file['tmp_name'];
        $picname = $upload_file['name'];
        $houzhui = substr(strrchr($picname, '.'), 1);
        $title = basename($picname, '.'.$houzhui);
        // 操作 执行上传
        $result = $uploadServices->doUpload('image', $picname, $pictemp, $pictype, $picsize, $title);
        $this->result(200, '请求成功', $result['result_code'], $result['result_msg'], $result['resultData']);
    }
    /**
     * @xk 小程序图片上传
     * @method  uploadWxappImg
     * @param   UploadServices $uploadServices  [description]
     * @return  [type] [description]
     * @Version V5.0
     * @Time    2021-11-25
     * @City    青岛
     * @Author  xkyinzhe <zhonghuaxinxing@sina.cn>
     */
    public function uploadWxappImg(UploadServices $uploadServices){
      $upload_file = $_FILES['file'];
      // 获取图片基础信息
      $picsize = $upload_file['size'];
      $pictype = $upload_file['type'];
      $pictemp = $upload_file['tmp_name'];
      $picname = $upload_file['name'];
      $houzhui = substr(strrchr($picname, '.'), 1);
      $title = basename($picname, '.'.$houzhui);
      // 操作 执行上传
      $result = $uploadServices->doUpload('image', $picname, $pictemp, $pictype, $picsize, $title);
      return Json($result['resultData']['upload_url']);
    }
    /**
     * @xk 视频上传
     *
     * @method uploadVideo
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-15T09:27:28+080
     */
    public function uploadVideo(UploadServices $uploadServices)
    {
        $upload_file = $_FILES['file'];
        // 获取图片基础信息
        $picsize = $upload_file['size'];
        $pictype = $upload_file['type'];
        $pictemp = $upload_file['tmp_name'];
        $picname = $upload_file['name'];
        $houzhui = substr(strrchr($picname, '.'), 1);
        $title = basename($picname, '.'.$houzhui);
        // 操作 执行上传
        $result = $uploadServices->doUpload('video', $picname, $pictemp, $pictype, $picsize, $title);
        $this->result(200, '请求成功', $result['result_code'], $result['result_msg'], $result['resultData']);
    }
    /**
     * @xk 微信小程序视频上传
     * @method  uploadWxappVideo
     * @param   UploadServices $uploadServices  [description]
     * @return  [type] [description]
     * @Version V5.0
     * @Time    2021-11-25
     * @City    青岛
     * @Author  xkyinzhe <zhonghuaxinxing@sina.cn>
     */
    public function uploadWxappVideo(UploadServices $uploadServices)
    {
        $upload_file = $_FILES['file'];
        // 获取图片基础信息
        $picsize = $upload_file['size'];
        $pictype = $upload_file['type'];
        $pictemp = $upload_file['tmp_name'];
        $picname = $upload_file['name'];
        $houzhui = substr(strrchr($picname, '.'), 1);
        $title = basename($picname, '.'.$houzhui);
        // 操作 执行上传
        $result = $uploadServices->doUpload('video', $picname, $pictemp, $pictype, $picsize, $title);
        return Json($result['resultData']['upload_url']);
    }

    /**
     * @xk 文件上传
     *
     * @method uploadFile
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-15T09:27:49+080
     */
    public function uploadFile(UploadServices $uploadServices)
    {
      $upload_file = $_FILES['file'];
      // 获取图片基础信息
      $picsize = $upload_file['size'];
      $pictype = $upload_file['type'];
      $pictemp = $upload_file['tmp_name'];
      $picname = $upload_file['name'];
      $houzhui = substr(strrchr($picname, '.'), 1);
      $title = basename($picname, '.'.$houzhui);
      // echo $pictype; die();
      // 操作 执行上传
      $result = $uploadServices->doUpload('file', $picname, $pictemp, $pictype, $picsize, $title);
      $this->result(200, '请求成功', $result['result_code'], $result['result_msg'], $result['resultData']);
    }

    /**
     * @xk 清除图片
     *
     * @method clearImg
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-15T16:28:52+080
     */
    public function clearImg(UploadServices $uploadServices)
    {
        $data = $this->request->param();
        $result = $uploadServices->destroyUrl($data['path']);
        $this->result(200, '请求成功', $result['result_code'], $result['result_msg']);
    }
    /**
     * @xk base64 图片上传
     *
     * @method uploadBase64
     * @return [type]                  [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-25T17:55:22+080
     */
    public function uploadBase64(UploadServices $uploadServices)
    {
        $param = request()->param();
        $base64_img = isset($param['path']) ? $param['path'] : '';
        $result = $uploadServices->doBase64Upload('image', $base64_img);
        $this->result(200, '请求成功', $result['result_code'], $result['result_msg'], $result['resultData']);
    }
}
